package com.cardinalscientific.apps.speedwatcher;

import com.cardinalscientific.apps.speedwatcher.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;

public class SettingsActivity extends Activity {
	
	public static class SettingsFragment extends PreferenceFragment 
										 implements OnSharedPreferenceChangeListener {
		
		private static final String PREF_SPEED_THRESHOLD_KEY = "pref_key_speed_threshold";
		//private static final String TAG = "SettingsActivity"; 
		
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        
			super.onCreate(savedInstanceState);
	        addPreferencesFromResource(R.xml.preferences);
	        
	        //Set for first time
	        setOutputDevice(getPreferenceManager().getSharedPreferences(), 
	        		this.getResources().getString(R.string.pref_devices_key));
	        setSpeedUnit(getPreferenceManager().getSharedPreferences(),
	        		this.getResources().getString(R.string.pref_speed_unit_key));
	    }
		
		@Override
		public void onResume() {
			super.onResume();
		    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		}

		@Override
		public void onPause() {
			super.onPause();
		    getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		}

		@Override
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			
			if(key.equals(this.getResources().getString(R.string.pref_devices_key))){
				setOutputDevice(sharedPreferences, key);
			}
			
			if(key.equals(this.getResources().getString(R.string.pref_speed_unit_key))) {
				setSpeedUnit(sharedPreferences, key);
			}
			
			/*if(key.equals(this.getResources().getString(R.string.pref_speed_threshold_key))) {
				SeekBarPreference sp = (SeekBarPreference) this.findPreference(key);
				int threshold = sp.getSharedPreferences().getInt(key, 0);
			}*/
			
			/*if(key.equals(this.getResources().getString(R.string.pref_alert_freq_key))) {
				SeekBarPreference sp = (SeekBarPreference) this.findPreference(key);
				int alertFreq = sp.getSharedPreferences().getInt(key, 0);
			}*/
		}
		
		private void setOutputDevice(SharedPreferences sharedPreferences, String key) {
			
			ListPreference lp = (ListPreference) this.findPreference(key);
			lp.setSummary(lp.getEntry().toString());
			
		}
		
		private void setSpeedUnit(SharedPreferences sharedPreferences, String key) {
			
			ListPreference lp = (ListPreference) this.findPreference(key);
			lp.setSummary(lp.getEntry().toString());
			
			SeekBarPreference sp = (SeekBarPreference) this.findPreference(PREF_SPEED_THRESHOLD_KEY);;
			String unit = sharedPreferences.getString(this.getResources().getString(R.string.pref_speed_unit_key), "0");
			if(unit.equals("1")) sp.setUnitsRight(" kmh");
			else 				 sp.setUnitsRight(" mph");
		}
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction()
        .replace(android.R.id.content, new SettingsFragment())
        .commit();
    }
}
